﻿using System.Collections.Generic;
using System.Data.Common;
using Jayden.Dll.Core.Persistence;
using Jayden.Dll.Core.Persistence.Data;
using Jayden.Dll.Core.Persistence.Data.SQLite;
using Jayden.Dll.Core.Persistence.Models;

namespace Jayden.Dll.Core.Membership
{
	public class SQLiteUserRoleDataAccess : SQLiteDataAccess<UserRole>, UserRoleDataAccess
	{
		public SQLiteUserRoleDataAccess(Configuration config, UnitOfWork uow, Entity entity) : base(config, uow, entity) { }

		public UserRole Read(string username, string rolename) { return Read(UnitOfWork.NewObjectId(Entity, username, rolename)); }

		public UserRole Create(string username, string rolename)
		{
			UserRole newUserRole = new UserRole(UnitOfWork);
			newUserRole.UserName = username;
			newUserRole.RoleName = rolename;
			Create(newUserRole);
			return newUserRole;
		}

		public void DeleteByUserName(string username)
		{
			string deleteCommandText = Helper.GenerateDelete(Entity.Table.TableName, "UserName=@UserName");
			using (DbCommandProxy deleteCommand = CreateCommand(deleteCommandText))
			{
				CreateParameter(deleteCommand, "UserName", username);
				int count = deleteCommand.Command.ExecuteNonQuery();
			}
		}

		public void DeleteByRoleName(string rolename)
		{
			string deleteCommandText = Helper.GenerateDelete(Entity.Table.TableName, "RoleName=@RoleName");
			using (DbCommandProxy deleteCommand = CreateCommand(deleteCommandText))
			{
				CreateParameter(deleteCommand, "RoleName", rolename);
				int count = deleteCommand.Command.ExecuteNonQuery();
			}
		}

		public List<UserRole> FindByUserName(string username)
		{
			string selectCommandText = Helper.GenerateSelect(null, Entity.Table.TableName, "UserName=@UserName", null, null, null, null);
			using (DbCommandProxy selectCommand = CreateCommand(selectCommandText))
			{
				CreateParameter(selectCommand, "UserName", username);
				using (DbDataReader reader = selectCommand.Command.ExecuteReader())
				{
					List<UserRole> userRoles = new List<UserRole>(EnumerateFromReader(reader));
					return userRoles;
				}
			}
		}

		public List<UserRole> FindByRoleName(string rolename)
		{
			string selectCommandText = Helper.GenerateSelect(null, Entity.Table.TableName, "RoleName=@RoleName", null, null, null, null);
			using (DbCommandProxy selectCommand = CreateCommand(selectCommandText))
			{
				CreateParameter(selectCommand, "RoleName", rolename);
				using (DbDataReader reader = selectCommand.Command.ExecuteReader())
				{
					List<UserRole> userRoles = new List<UserRole>(EnumerateFromReader(reader));
					return userRoles;
				}
			}
		}


		public void Delete(string username, string rolename) { Delete(UnitOfWork.NewObjectId(Entity, username, rolename)); }


		public List<UserRole> FindByRoleName(string rolename, string usernameLike)
		{
			string selectCommandText = Helper.GenerateSelect(null, Entity.Table.TableName, "RoleName=@RoleName AND UserName LIKE @UserName", null, null, null, null);
			using (DbCommandProxy selectCommand = CreateCommand(selectCommandText))
			{
				CreateParameter(selectCommand, "RoleName", rolename);
				CreateParameter(selectCommand, "UserName", usernameLike);
				using (DbDataReader reader = selectCommand.Command.ExecuteReader())
				{
					List<UserRole> userRoles = new List<UserRole>(EnumerateFromReader(reader));
					return userRoles;
				}
			}
		}
	}
}
